<?php
/*
 * Copyright (C) 2009 Jeffrey Palm
 */

$events = readEvents('data.xml');

// Build a map from names to list of events
$names2events = array();

// Keep the letters to make a menu
$letters = array();
foreach ($events as $e) {
  $lst = $names2events[$e->getTitle()];
  if (!$lst) {
    $lst = array();
  }
  $lst []= $e;
  $names2events[$e->getTitle()] = $lst;
  $fst = strtoupper(substr($e->getTitle(),0,1));
  $letters[$fst] = TRUE;
}

ksort($names2events);
ksort($letters);

// Menu
$i = 0;
echo '<p id="letters">';
foreach ($letters as $l=>$ignore) {
  echo '<a href="#_' . $l . '">' . $l . '</a>';
  if ($i<count($letters)-1) echo ' ';
  $i++;
}
echo '</p>';

// Print out the list of events
echo '<p>';
$i = 1;
foreach ($names2events as $name => $events) {
  $fst = strtoupper(substr($name,0,1));
  if ($letters[$fst]) {
    echo '<a name="_' . $fst . '"></a>';
    echo '<div class="tableTitle"><span class="tableTitleLeft">', $fst, 
      '</span><span class="tableTitleRight"> ', '<a href="#top">', 'top', '</a>', '</span>', '</div>';
    $letters[$fst] = FALSE;
  }
  $id = '_id_' . ($i++);
  printEvents($events,$id,$name);
}
echo '</p>';

?>